<?php

namespace App\Http\Controllers\Manage;

use App\Helpers\ResponseData;
use App\Http\Controllers\Controller;
use App\Http\Requests\PriceRangeRequest;
use App\Models\PriceRange;
use App\Models\QuotaionInformation;
use Illuminate\Http\Request;
//设置价格区间
class PriceRangeController extends Controller
{
    use ResponseData;
    public function store(PriceRange $model,PriceRangeRequest $request){
        if(!$request->filled('group_id')){
            return $this->failed('group_id 不能为空',201);
        }

        if($request->min>$request->max){
            return $this->failed('最小区间不能大于最大区间',201);
        }

        $data = $request->all();
        $model->fill($data);
        $model->save();
        return $this->success($model);
    }
    //更新
    public function update(PriceRange $model,Request $request){
        $data = $model->find($request->id);

        if($data->min!=$request->min){
            $data->old_min = $data->min;
        }
        if($data->max!=$request->max){
            $data->old_max = $data->max;
        }
        if($data->price!=$request->price){
            $data->old_price = $data->price;
        }
        if($request->filled('min')){
            $data->min = $request->min;
        }
        if($request->filled('max')){
            $data->max = $request->max;
        }
        if($request->filled('price')){
            $data->price = $request->price;
        }
        $data->save();
        return $this->success($data);
    }
    public function destory(PriceRange $model,Request $request){
        $data = $model->find($request->id);
        if(!$data){
            return $this->failed('数据不存在',201);
        }
        $data->delete();
        return $this->success('删除成功');
    }
}
